package com.sky.controller.user;

import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author iTaotao
 * @version 1.0
 * @Description 地址簿控制器
 * @createTime 2023年08月03日 09:34:00
 */
@RestController
@RequiredArgsConstructor
@Slf4j
@RequestMapping("/user/addressBook")
public class AddressBookController {
    private final AddressBookService addressBookService;

    /**
     * 地址列表
     *
     * @return {@link Result}<{@link List}<{@link AddressBook}>>
     */
    @GetMapping("/list")
    public Result<List<AddressBook>> list() {
        List<AddressBook> addressBookList = addressBookService.list();
        return Result.success(addressBookList);
    }

    /**
     * 添加地址
     *
     * @param addressBook 地址本
     * @return {@link Result}<{@link ?}>
     */
    @PostMapping
    public Result<?> addAddress(@RequestBody AddressBook addressBook) {
        addressBookService.addAddress(addressBook);
        return Result.success();
    }

    /**
     * 设置默认地址
     *
     * @param addressBook 地址本
     * @return {@link Result}<{@link ?}>
     */
    @PutMapping("/default")
    public Result<?> setDefault(@RequestBody AddressBook addressBook) {
        addressBookService.setDefault(addressBook);
        return Result.success();
    }

    /**
     * 查询默认地址
     *
     * @return {@link Result}<{@link List}<{@link AddressBook}>>
     */
    @GetMapping("/default")
    public Result<List<AddressBook>> selectDefaultAddressBook() {
        List<AddressBook> addressBookList = addressBookService.selectDefaultAddressBook();
        return Result.success(addressBookList);

    }

    /**
     * 更新地址通过id
     *
     * @param addressBook 地址本
     * @return {@link Result}<{@link ?}>
     */
    @PutMapping
    public Result<?> updateAddressBookById(@RequestBody AddressBook addressBook) {
        addressBookService.updateAddressBookById(addressBook);
        return Result.success();
    }

    /**
     * 查询地址通过id
     *
     * @param id id
     * @return {@link Result}<{@link AddressBook}>
     */
    @GetMapping("/{id}")
    public Result<AddressBook> selectAddressById(@PathVariable Long id) {

        AddressBook addressBook = addressBookService.selectAddressById(id);
        return Result.success(addressBook);
    }

    @DeleteMapping
    public Result<?> removeAddressById(Long id){
        addressBookService.removeAddressById(id);
        return Result.success();

    }
}
